<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once ('Entity/News_Entity.php');
class News_Model extends News_Entity
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function dataSort($field, $order)
    {
        $xOrder = $order == 'asc' ? 'desc' : 'asc';
        $img = $order == 'desc' ? '<img src=\'/data/admin/down.jpg\' />' : ' <img src=\'/data/admin/up.jpg\' />';
        $url = anchor('news/sort/' . $field . '/' . $xOrder, t($field) . $img);
        $_SESSION['sm_sort_news'] = array(
            'field' => $field,
            'order' => $order,
            'url' => $url);
    }

    public function dataIndex($target)
    {
        $numshow = 25;
        $this->db->select('news.id, news.title, news_types.title as tid_type, news.status, news.hit, news.created');
        $this->db->from('news');

        $this->db->join('news_types', 'news.tid = news_types.id');

        if ($this->session->userdata('sm_filter_news_types'))
            $this->db->where('news.tid', $this->session->userdata('sm_filter_news_types'));
        if ($this->session->userdata('sm_find_news')) {
            $this->db->like('news.title', $this->session->userdata('sm_find_news'));
        }
        if (isset($_SESSION['sm_sort_news'])) {
            $sort = $_SESSION['sm_sort_news'];
            $this->db->order_by('news.' . $sort['field'], $sort['order']);
        } else {
            $this->db->order_by('news.created', 'desc');
        }
        $this->db->limit($numshow, $target);
        $query = $this->db->get();
        $paging = $this->home_model->paging_Admin('news/index', $this->_total(), $numshow,
            4);
        return array(
            'query' => $query,
            'paging' => $paging,
            'fil_news_types' => $this->Filter_news_types(),
            'formFind' => $this->formFind());
    }

    private function cbo_news_types($id = 0)
    {
        $this->load->helper("form");
        $query = $this->db->get("news_types");
        foreach ($query->result() as $row) {
            $options[$row->id] = $row->title;
        }
        $sl = $id == 0 ? set_value("tid") : $id;
        return form_dropdown("tid", $options, $sl);
    }

    private function _total()
    {
        return $this->db->count_all_results("news");
    }

    private function _checknewsById($id)
    {
        $query = $this->db->get_where("news", array("id" => $id));
        if ($query->num_rows() > 0)
            return true;
        else {
            notify_set_messages(notify_atten(t("Record không tồn tại")));
            return false;
        }
    }

    private function formFind()
    {
        $this->load->helper('form');
        $attributes = array('name' => 'frmFind', 'id' => 'frmFind');
        $f = form_open(site_url('news/find'), $attributes);
        $data = array(
            'name' => 'txt_find',
            'id' => 'txt_find',
            'value' => '',
            'style' => 'width:200px');
        $f .= form_input($data);
        $s = array(
            'name' => 'submit',
            'value' => t('Find'),
            'class' => 'ibutton',
            'style' => 'margin-left:10px');
        $f .= form_submit($s);
        if ($this->session->userdata('sm_find_news')) {
            $r = array(
                'name' => 'btnReset',
                'value' => 'Reset',
                'class' => 'ibutton red',
                'style' => 'margin-left:10px');
            $f .= form_submit($r);
        }
        $f .= form_close();
        return $f;
    }

    private function Filter_news_types()
    {
        $this->load->helper('form');
        $query = $this->db->get('news_types');
        foreach ($query->result() as $row) {
            $options[$row->id] = $row->title;
        }
        $id = !$this->session->userdata('sm_filter_news_types') ? 0 : $this->session->
            userdata('sm_filter_news_types');
        $attributes = array('name' => 'frmFilternews_types', 'id' =>
                'frmFilternews_types');
        $f = form_open(site_url('news/filternews_types'), $attributes);
        $f .= form_dropdown('tid', $options, $id);
        $data = array(
            'name' => 'btnFilternews_types',
            'class' => 'ibutton green',
            'style' => 'margin-left:10px',
            'value' => t('Filter'));
        $f .= form_submit($data);
        if ($this->session->userdata('sm_filter_news_types')) {
            $r = array(
                'name' => 'btnReset',
                'class' => 'ibutton red',
                'style' => 'margin-left:10px',
                'value' => 'Reset');
            $f .= form_submit($r);
        }
        $f .= form_close();
        return $f;
    }


    public function dataEdit($id)
    {
        if (!$this->_checknewsById($id))
            return redirect(site_url('news'));
        $this->load->helper('editor');
        $query = $this->db->get_where('news', array('id' => $id));
        return array('query' => $query, 'cbo_news_types' => $this->cbo_news_types($query->
                row('tid')));
    }

    public function dataAdd()
    {
        $this->load->helper('editor');
        return array('cbo_news_types' => $this->cbo_news_types());
    }

    public function checkForm()
    {
        $this->form_validation->set_rules('title', t('title'), 'trim|required');
        $this->form_validation->set_rules('alias', t('alias'), 'trim|required');
        $this->form_validation->set_rules('image', t('image'), 'trim|required');
        $this->form_validation->set_rules('summary', t('summary'), 'trim|required');
        $this->form_validation->set_rules('content', t('content'), 'trim|required');
        $this->form_validation->set_rules('status', t('status'), 'trim|required');
        $this->form_validation->set_rules('hot', t('hot'), 'trim|required');
        $this->form_validation->set_rules('order', t('order'), 'trim|required|integer');
        $this->form_validation->set_rules('tid', t('tid'), 'trim|required|integer');
        $this->form_validation->set_rules('meta_tag', t('meta_tag'), 'trim|required');
        $this->form_validation->set_rules('meta_description', t('meta_description'),
            'trim|required');
        return $this->form_validation->run();
    }

    public function doUpdate()
    {
        $this->load->helper('text');
        $this->setTid($this->input->post('tid'));
        $this->setTitle($this->input->post('title'));
        $this->setAlias($this->input->post('alias'));
        $this->setImage($this->input->post('image'));
        $this->setSummary($this->input->post('summary'));
        $this->setContent($this->input->post('content'));
        $this->setHot($this->input->post('hot'));
        $this->setOrder($this->input->post('order'));
        $this->setStatus($this->input->post('status'));
        $this->setMeta_tag($this->input->post('meta_tag'));
        $this->setMeta_description($this->input->post('meta_description'));

        if (isset($_POST['idx'])) {
            $id = decrypt($_POST['idx']);
            $this->setId($id);
            $this->setChanged(time());
        } else {
            $this->setCreated(time());
        }
        return $this->save();
    }

}
